home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sharutil.2 / sharutil / sharutils-4.2 / src / shar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-03  |  52.1 KB  |  2,131 lines

  1. /* Handle so called `shell archives'.
  2.    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software Foundation,
  16.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #include "system.h"
  20.  
  21. static const char *cut_mark_line
  22.   = "---- Cut Here and feed the following to sh ----\n";
  23.  
  24. /* Delimiter to put after each file.  */
  25. #define    DEFAULT_HERE_DELIMITER "SHAR_EOF"
  26.  
  27. /* Character which goes in front of each line.  */
  28. #define DEFAULT_LINE_PREFIX_1 'X'
  29.  
  30. /* Character which goes in front of each line if here_delimiter[0] ==
  31.    DEFAULT_LINE_PREFIX_1.  */
  32. #define DEFAULT_LINE_PREFIX_2 'Y'
  33.  
  34. /* Shell command able to count characters from its standard input.  We
  35.    have to take care for the locale setting because wc in multi-byte
  36.    character environments get different results.  */
  37. #define CHARACTER_COUNT_COMMAND "LC_ALL= LC_CTYPE= LANG= wc -c <"
  38.  
  39. /* Command computing MD5 sumof specified file.  Because there is not
  40.    official standard for this we have to restrict ourself to a few
  41.    versions: GNU md5sum from GNU fileutils and the version by
  42.    Plumb/Lankester.  */
  43. #define MD5SUM_COMMAND "md5sum"
  44.  
  45. /* Maximum length for a text line before it is considered binary.  */
  46. #define MAXIMUM_NON_BINARY_LINE 200
  47.  
  48. /* System related declarations.  */
  49.  
  50. #include <ctype.h>
  51.  
  52. #if STDC_HEADERS
  53. # define ISASCII(Char) 1
  54. #else
  55. # ifdef isascii
  56. #  define ISASCII(Char) isascii (Char)
  57. # else
  58. #  if HAVE_ISASCII
  59. #   define ISASCII(Char) isascii (Char)
  60. #  else
  61. #   define ISASCII(Char) ((Char) & 0x7f == (unsigned char) (Char))
  62. #  endif
  63. # endif
  64. #endif
  65.  
  66. #include <time.h>
  67.  
  68. struct tm *localtime ();
  69.  
  70. #if !NO_WALKTREE
  71.  
  72. /* Declare directory reading routines and structures.  */
  73.  
  74. #ifdef __MSDOS__
  75. # include "msd_dir.h"
  76. # define NAMLEN(dirent) ((dirent)->d_namlen)
  77. #else
  78. # if HAVE_DIRENT_H
  79. #  include <dirent.h>
  80. #  define NAMLEN(dirent) (strlen((dirent)->d_name))
  81. # else
  82. #  define dirent direct
  83. #  define NAMLEN(dirent) ((dirent)->d_namlen)
  84. #  if HAVE_SYS_NDIR_H
  85. #   include <sys/ndir.h>
  86. #  endif
  87. #  if HAVE_SYS_DIR_H
  88. #   include <sys/dir.h>
  89. #  endif
  90. #  if HAVE_NDIR_H
  91. #   include <ndir.h>
  92. #  endif
  93. # endif
  94. #endif
  95.  
  96. DIR *opendir ();
  97. struct dirent *readdir ();
  98.  
  99. #endif /* !NO_WALKTREE */
  100.  
  101. /* Option variables.  */
  102.  
  103. #include "getopt.h"
  104. #include "md5.h"
  105.  
  106. /* No Brown-Shirt mode.  */
  107. static int vanilla_operation_mode = 0;
  108.  
  109. /* Mixed text and binary files.  */
  110. static int mixed_uuencoded_file_mode = -1;
  111.  
  112. /* Flag for binary files.  */
  113. static int uuencoded_file_mode = -1;
  114.  
  115. /* Run input files through gzip (requires uuencoded_file_mode).  */
  116. static int gzipped_file_mode = -1;
  117.  
  118. /* -N option to gzip.  */
  119. static int gzip_compression_level = 9;
  120.  
  121. /* Run input files through compress (requires uuencoded_file_mode).  */
  122. static int compressed_file_mode = -1;
  123.  
  124. /* -bN option to compress */
  125. static int bits_per_compressed_byte = 12;
  126.  
  127. /* Generate $shar_touch commands.  */
  128. static int timestamp_mode = 1;
  129.  
  130. /* Option to provide wc checking.  */
  131. static int character_count_mode = 1;
  132.  
  133. /* Option to provide MD5 sum checking.  */
  134. static int md5_count_mode = 1;
  135.  
  136. /* Use temp file instead of pipe to feed uudecode.  This gives better
  137.    error detection, at expense of disk space.  This is also necessary for
  138.    those versions of uudecode unwilling to read their standard input.  */
  139. static int inhibit_piping_mode = 0;
  140.  
  141. /* --no-i18n option to prevent internationalized shell archives.  */
  142. static int no_i18n;
  143.  
  144. /* Character to get at the beginning of each line.  */
  145. static int line_prefix;
  146.  
  147. /* Option to generate "Archive-name:" headers.  */
  148. static int net_headers_mode = 0;
  149.  
  150. /* Documentation name for archive.  */
  151. static char *archive_name = NULL;
  152.  
  153. /* Option to provide append feedback at shar time.  */
  154. static int quiet_mode = 0;
  155.  
  156. /* Option to provide extract feedback at unshar time.  */
  157. static int quiet_unshar_mode = 0;
  158.  
  159. /* Pointer to delimiter string.  */
  160. static const char *here_delimiter = DEFAULT_HERE_DELIMITER;
  161.  
  162. /* Value of strlen (here_delimiter).  */
  163. static size_t here_delimiter_length;
  164.  
  165. /* Use line_prefix even when first char does not force it.  */
  166. static int mandatory_prefix_mode = 0;
  167.  
  168. /* Option to provide cut mark.  */
  169. static int cut_mark_mode = 0;
  170.  
  171. /* Check if file exists.  */
  172. static int check_existing_mode = 1;
  173.  
  174. /* Interactive overwrite.  */
  175. static int query_user_mode = 0;
  176.  
  177. /* Allow positional parameters.  */
  178. static int intermixed_parameter_mode = 0;
  179.  
  180. /* Strip directories from filenames.  */
  181. static int basename_mode;
  182.  
  183. /* Switch for debugging on.  */
  184. #if DEBUG
  185. static int debugging_mode = 0;
  186. #endif
  187.  
  188. /* Split files in the middle.  */
  189. static int split_file_mode = 0;
  190.  
  191. /* File size limit in kilobytes.  */
  192. static unsigned file_size_limit = 0;
  193.  
  194. /* Other global variables.  */
  195.  
  196. /* The name this program was run with. */
  197. const char *program_name;
  198.  
  199. /* If non-zero, display usage information and exit.  */
  200. static int show_help = 0;
  201.  
  202. /* If non-zero, print the version on standard output and exit.  */
  203. static int show_version = 0;
  204.  
  205. /* File onto which the shar script is being written.  */
  206. static FILE *output = NULL;
  207.  
  208. /* Position for archive type message.  */
  209. static off_t archive_type_position;
  210.  
  211. /* Position for first file in the shar file.  */
  212. static long first_file_position;
  213.  
  214. /* Base for output filename.  FIXME: No fix limit in GNU... */
  215. static char output_base_name[50];
  216.  
  217. /* Actual output filename.  FIXME: No fix limit in GNU... */
  218. static char output_filename[50];
  219.  
  220. static char *submitter_address = NULL;
  221.  
  222. /* Output file ordinal.  FIXME: also flag for -o.  */
  223. static int part_number = 0;
  224.  
  225. /* Table saying whether each character is binary or not.  */
  226. static unsigned char byte_is_binary[256];
  227.  
  228. /* For checking file type and access modes.  */
  229. static struct stat struct_stat;
  230.  
  231. /* Nonzero if special NLS option (--print-text-domain-dir) is selected.  */
  232. static int print_text_dom_dir;
  233.  
  234. /* The number used to make the intermediate files unique.  */
  235. static int sharpid;
  236.  
  237. #if DEBUG
  238. # define DEBUG_PRINT(Format, Value) \
  239.     if (debugging_mode) printf(Format, Value)
  240. #else
  241. # define DEBUG_PRINT(Format, Value)
  242. #endif
  243.  
  244. static void open_output __P ((void));
  245. static void close_output __P ((void));
  246. static void usage __P ((int));
  247.  
  248. /* Walking tree routines.  */
  249.  
  250. /* Define a type just for easing ansi2knr's life.  */
  251. typedef int (*walker_t) __P ((const char *, const char *));
  252.  
  253. #if !NO_WALKTREE
  254.  
  255. /*--------------------------------------------------------------------------.
  256. | Recursively call ROUTINE on each entry, down the directory tree.  NAME    |
  257. | is the path to explore.  RESTORE_NAME is the name that will be later        |
  258. | relative to the unsharing directory.  ROUTINE may also assume            |
  259. | struct_stat is set, it accepts updated values for NAME and RESTORE_NAME.  |
  260. `--------------------------------------------------------------------------*/
  261.  
  262. static int
  263. walkdown (routine, local_name, restore_name)
  264.      walker_t routine;
  265.      const char *local_name;
  266.      const char *restore_name;
  267. {
  268.   DIR *directory;        /* directory being scanned */
  269.   struct dirent *entry;        /* current entry in directory */
  270.   int status;            /* status to return */
  271.  
  272.   char *local_name_copy;    /* writeable copy of local_name */
  273.   size_t local_name_length;    /* number of characters in local_name_copy */
  274.   size_t sizeof_local_name;    /* allocated size of local_name_copy */
  275.  
  276.   char *restore_name_copy;    /* writeable copy of restore_name */
  277.   size_t restore_name_length;    /* number of characters in restore_name_copy */
  278.   size_t sizeof_restore_name;    /* allocated size of restore_name_copy */
  279.  
  280.   if (stat (local_name, &struct_stat))
  281.     {
  282.       error (0, errno, local_name);
  283.       return 1;
  284.     }
  285.  
  286.   if (!S_ISDIR (struct_stat.st_mode & S_IFMT))
  287.     return (*routine) (local_name, restore_name);
  288.  
  289.   if (directory = opendir (local_name), !directory)
  290.     {
  291.       error (0, errno, local_name);
  292.       return 1;
  293.     }
  294.  
  295.   status = 0;
  296.  
  297.   local_name_copy = xstrdup (local_name);
  298.   local_name_length = strlen (local_name_copy);
  299.   sizeof_local_name = local_name_length + 1;
  300.  
  301.   restore_name_copy = xstrdup (restore_name);
  302.   restore_name_length = strlen (restore_name_copy);
  303.   sizeof_restore_name = restore_name_length + 1;
  304.  
  305.   while (!status && (entry = readdir (directory), entry))
  306.     if (strcmp (entry->d_name, ".") && strcmp (entry->d_name, ".."))
  307.       {
  308.     int added_size = 1 + NAMLEN (entry);
  309.  
  310.     /* Update file names, reallocating them as required.  */
  311.  
  312.     if (local_name_length + added_size + 1 > sizeof_local_name)
  313.       {
  314.         sizeof_local_name = local_name_length + added_size + 1;
  315.         local_name_copy = (char *)
  316.           xrealloc (local_name_copy, sizeof_local_name);
  317.       }
  318.     sprintf (local_name_copy + local_name_length, "/%s", entry->d_name);
  319.  
  320.     if (restore_name_length + added_size + 1 > sizeof_restore_name)
  321.       {
  322.         sizeof_restore_name = restore_name_length + added_size + 1;
  323.         restore_name_copy = (char *)
  324.           xrealloc (restore_name_copy, sizeof_restore_name);
  325.       }
  326.     sprintf (restore_name_copy + restore_name_length, "/%s",
  327.          entry->d_name);
  328.  
  329.     /* Avoid restoring `./xxx' when shar'ing `.'.  */
  330.  
  331.     if (!strncmp (restore_name, "./", 2))
  332.       {
  333.         strcpy (restore_name_copy, restore_name_copy + 2);
  334.         restore_name_length -= 2;
  335.       }
  336.  
  337.     status = walkdown (routine, local_name_copy, restore_name_copy);
  338.       }
  339.  
  340.   /* Clean up.  */
  341.  
  342.   if (sizeof_local_name > 0)
  343.     free (local_name_copy);
  344.   if (sizeof_restore_name > 0)
  345.     free (restore_name_copy);
  346.  
  347. #if CLOSEDIR_VOID
  348.   closedir (directory);
  349. #else
  350.   if (closedir (directory))
  351.     {
  352.       error (0, errno, local_name);
  353.       return 1;
  354.     }
  355. #endif
  356.  
  357.   return status;
  358. }
  359.  
  360. #endif /* !NO_WALKTREE */
  361.  
  362. /*------------------------------------------------------------------.
  363. | Walk through the directory tree, calling ROUTINE for each entry.  |
  364. | ROUTINE may also assume struct_stat is set.                |
  365. `------------------------------------------------------------------*/
  366.  
  367. static int
  368. walktree (routine, local_name)
  369.      walker_t routine;
  370.      const char *local_name;
  371. {
  372.   const char *restore_name;
  373.   char *local_name_copy;
  374.   char *cursor;
  375.   int status;
  376.   int len;
  377.  
  378.   /* Remove crumb at end.  */
  379.  
  380.   len = strlen (local_name);
  381.   local_name_copy = (char *) alloca (len + 1);
  382.   memcpy (local_name_copy, local_name, len + 1);
  383.   cursor = local_name_copy + len - 1;
  384.   while (*cursor == '/' && cursor > local_name_copy)
  385.     *cursor-- = '\0';
  386.  
  387.   /* Remove crumb at beginning.  */
  388.  
  389.   if (basename_mode)
  390.     restore_name = basename (local_name_copy);
  391.   else if (!strncmp (local_name_copy, "./", 2))
  392.     restore_name = local_name_copy + 2;
  393.   else
  394.     restore_name = local_name_copy;
  395.  
  396. #if NO_WALKTREE
  397.  
  398.   /* Just act on current entry.  */
  399.  
  400.   status = stat (local_name_copy, &struct_stat);
  401.   if (status != 0)
  402.     error (0, errno, local_name_copy);
  403.   else
  404.     status = (*routine) (local_name_copy, restore_name);
  405.  
  406. #else
  407.  
  408.   /* Walk recursively.  */
  409.  
  410.   status = walkdown (routine, local_name_copy, restore_name);
  411.  
  412. #endif
  413.  
  414.   return status;
  415. }
  416.  
  417. /* Generating parts of shar file.  */
  418.  
  419. /*---------------------------------------------------------------------.
  420. | Build a `drwxrwxrwx' string corresponding to MODE into MODE_STRING.  |
  421. `---------------------------------------------------------------------*/
  422.  
  423. static char *
  424. mode_string (mode)
  425.      unsigned mode;
  426. {
  427.   static char result[12];
  428.  
  429.   strcpy (result, "----------");
  430.  
  431.   if (mode & 00400)
  432.     result[1] = 'r';
  433.   if (mode & 00200)
  434.     result[2] = 'w';
  435.   if (mode & 00100)
  436.     result[3] = 'x';
  437.   if (mode & 04000)
  438.     result[3] = 's';
  439.   if (mode & 00040)
  440.     result[4] = 'r';
  441.   if (mode & 00020)
  442.     result[5] = 'w';
  443.   if (mode & 00010)
  444.     result[6] = 'x';
  445.   if (mode & 02000)
  446.     result[6] = 's';
  447.   if (mode & 00004)
  448.     result[7] = 'r';
  449.   if (mode & 00002)
  450.     result[8] = 'w';
  451.   if (mode & 00001)
  452.     result[9] = 'x';
  453.  
  454.   return result;
  455. }
  456.  
  457. /*-----------------------------------------------------------------------.
  458. | Generate shell code which, at *unshar* time, will study the properties |
  459. | of the unpacking system and set some variables accordingly.         |
  460. `-----------------------------------------------------------------------*/
  461.  
  462. static void
  463. generate_configure ()
  464. {
  465.   if (no_i18n)
  466.     fputs ("echo=echo\n", output);
  467.   else
  468.     {
  469.       fprintf (output, "\
  470. save_IFS=\"${IFS}\"\n\
  471. IFS=\"${IFS}:\"\n\
  472. gettext_dir=FAILED\n\
  473. locale_dir=FAILED\n\
  474. first_param=\"$1\"\n\
  475. for dir in $PATH\n\
  476. do\n\
  477.   if test \"$gettext_dir\" = FAILED && test -f $dir/gettext \\\n\
  478.      && ($dir/gettext --version >/dev/null 2>&1)\n\
  479.   then\n\
  480.     set `$dir/gettext --version 2>&1`\n\
  481.     if test \"$3\" = GNU\n\
  482.     then\n\
  483.       gettext_dir=$dir\n\
  484.     fi\n\
  485.   fi\n\
  486.   if test \"$locale_dir\" = FAILED && test -f $dir/%s \\\n\
  487.      && ($dir/%s --print-text-domain-dir >/dev/null 2>&1)\n\
  488.   then\n\
  489.     locale_dir=`$dir/%s --print-text-domain-dir`\n\
  490.   fi\n\
  491. done\n\
  492. IFS=\"$save_IFS\"\n\
  493. if test \"$locale_dir\" = FAILED || test \"$gettext_dir\" = FAILED\n\
  494. then\n\
  495.   echo=echo\n\
  496. else\n\
  497.   TEXTDOMAINDIR=$locale_dir\n\
  498.   export TEXTDOMAINDIR\n\
  499.   TEXTDOMAIN=sharutils\n\
  500.   export TEXTDOMAIN\n\
  501.   echo=\"$gettext_dir/gettext -s\"\n\
  502. fi\n",
  503.            "shar", "shar", "shar");
  504.       /* Above the name of the program of the package which supports the
  505.      --print-text-domain-dir option has to be given.  */
  506.     }
  507.  
  508.   if (query_user_mode)
  509.     if (vanilla_operation_mode)
  510.       fputs ("\
  511. shar_tty= shar_n= shar_c='\n\
  512. '\n",
  513.          output);
  514.     else
  515.       {
  516.  
  517.     /* Check if /dev/tty exists.  If yes, define shar_tty to
  518.        `/dev/tty', else, leave it empty.  */
  519.  
  520.     fputs ("\
  521. if test -n \"`ls /dev/tty 2>/dev/null`\"; then\n\
  522.   shar_tty=/dev/tty\n\
  523. else\n\
  524.   shar_tty=\n\
  525. fi\n",
  526.            output);
  527.  
  528.     /* Try to find a way to echo a message without newline.  Set
  529.        shar_n to `-n' or nothing for an echo option, and shar_c to
  530.        `\c' or nothing for a string terminator.  */
  531.  
  532.     fputs ("\
  533. if (echo \"testing\\c\"; echo 1,2,3) | grep c >/dev/null; then\n\
  534.   if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then\n\
  535.     shar_n= shar_c='\n\
  536. '\n\
  537.   else\n\
  538.     shar_n=-n shar_c=\n\
  539.   fi\n\
  540. else\n\
  541.   shar_n= shar_c='\\c'\n\
  542. fi\n",
  543.            output);
  544.       }
  545.  
  546.   if (timestamp_mode)
  547.     {
  548.       const char *file = "$$.touch";
  549.       const char *stamp = "1231235999";
  550.  
  551.       /* Set the shell variable shar_touch to `touch' if the touch
  552.      program is proven able to restore dates.  Otherwise, set
  553.      shar_touch to `:' and issue a warning.  */
  554.  
  555.       fprintf (output, "\
  556. touch -am %s %s >/dev/null 2>&1\n\
  557. if test ! -f %s && test -f %s; then\n\
  558.   shar_touch=touch\n\
  559. else\n\
  560.   shar_touch=:\n\
  561.   echo\n\
  562.   $echo '%s'\n\
  563.   $echo \"%s\"\n\
  564.   echo\n\
  565. fi\n\
  566. rm -f %s %s\n\
  567. #\n",
  568.            stamp, file, stamp, file,
  569.            N_("\
  570. WARNING: not restoring timestamps.  Consider getting and"),
  571.            N_("\
  572. installing GNU \\`touch', distributed in GNU File Utilities..."),
  573.            stamp, file);
  574.     }
  575.  
  576.   if (!split_file_mode || part_number == 1)
  577.     {
  578.       /* Create locking directory.  */
  579.       fprintf (output, "\
  580. if mkdir _sh%05d; then\n\
  581.   $echo 'x -' '%s'\n\
  582. else\n\
  583.   $echo '%s'\n\
  584.   exit 1\n\
  585. fi\n",
  586.            sharpid, N_("creating lock directory"),
  587.            N_("failed to create lock directory"));
  588.     }
  589. }
  590.  
  591. /*---.
  592. | ?  |
  593. `---*/
  594.  
  595. /* Ridiculously enough.  FIXME: No fix limit in GNU...  */
  596. #define MAX_MKDIR_ALREADY    128
  597.  
  598. char *mkdir_already[MAX_MKDIR_ALREADY];
  599. int mkdir_already_count = 0;
  600.  
  601. static void
  602. generate_mkdir (path)
  603.      const char *path;
  604. {
  605.   int counter;
  606.   char *cursor;
  607.  
  608.   /* If already generated code for this dir creation, don't do again.  */
  609.  
  610.   for (counter = 0; counter < mkdir_already_count; counter++)
  611.     if (!strcmp (path, mkdir_already[counter]))
  612.       return;
  613.  
  614.   /* Haven't done this one.  */
  615.  
  616.   if (mkdir_already_count == MAX_MKDIR_ALREADY)
  617.     error (EXIT_FAILURE, 0, _("Too many directories for mkdir generation"));
  618.   cursor = mkdir_already[mkdir_already_count++] = xmalloc (strlen (path) + 1);
  619.   strcpy (cursor, path);
  620.  
  621.   /* Generate the text.  */
  622.  
  623.   fprintf (output, "if test ! -d '%s'; then\n", path);
  624.   if (!quiet_unshar_mode)
  625.     fprintf (output, "  $echo 'x -' '%s' '%s'\n",
  626.          N_("creating directory"), path);
  627.   fprintf (output, "  mkdir '%s'\n", path);
  628.   fputs ("fi\n", output);
  629. }
  630.  
  631. /*---.
  632. | ?  |
  633. `---*/
  634.  
  635. static void
  636. generate_mkdir_script (path)
  637.      const char *path;
  638. {
  639.   char *cursor;
  640.  
  641.   for (cursor = strchr (path, '/'); cursor; cursor = strchr (cursor + 1, '/'))
  642.     {
  643.  
  644.       /* Avoid empty string if leading or double '/'.  */
  645.  
  646.       if (cursor == path || *(cursor - 1) == '/')
  647.     continue;
  648.  
  649.       /* Omit '.'.  */
  650.  
  651.       if (cursor[-1] == '.' && (cursor == path + 1 || cursor[-2] == '/'))
  652.     continue;
  653.  
  654.       /* Temporarily terminate string.  FIXME!  */
  655.  
  656.       *cursor = 0;
  657.       generate_mkdir (path);
  658.       *cursor = '/';
  659.     }
  660. }
  661.  
  662. /* Walking routines.  */
  663.  
  664. /*---.
  665. | ?  |
  666. `---*/
  667.  
  668. static int
  669. check_accessibility (local_name, restore_name)
  670.      const char *local_name;
  671.      const char *restore_name;
  672. {
  673.   if (access (local_name, 4))
  674.     {
  675.       error (0, 0, _("Cannot access %s"), local_name);
  676.       return 1;
  677.     }
  678.  
  679.   return 0;
  680. }
  681.  
  682. /*---.
  683. | ?  |
  684. `---*/
  685.  
  686. static int
  687. generate_one_header_line (local_name, restore_name)
  688.      const char *local_name;
  689.      const char *restore_name;
  690. {
  691.   fprintf (output, "# %6ld %s %s\n", struct_stat.st_size,
  692.        mode_string (struct_stat.st_mode), restore_name);
  693.   return 0;
  694. }
  695.  
  696. /*---.
  697. | ?  |
  698. `---*/
  699.  
  700. static void
  701. generate_full_header (argc, argv)
  702.      int argc;
  703.      char *const *argv;
  704. {
  705.   char *current_directory;
  706.   time_t now;
  707.   struct tm *local_time;
  708.   char buffer[80];        /* FIXME: No fix limit in GNU... */
  709.   int warned_once;
  710.   int counter;
  711.  
  712.   warned_once = 0;
  713.   for (counter = 0; counter < argc; counter++)
  714.     {
  715.  
  716.       /* Skip positional parameters.  */
  717.  
  718.       if (intermixed_parameter_mode &&
  719.       (strcmp (argv[counter], "-B") == 0 ||
  720.        strcmp (argv[counter], "-T") == 0 ||
  721.        strcmp (argv[counter], "-M") == 0 ||
  722.        strcmp (argv[counter], "-z") == 0 ||
  723.        strcmp (argv[counter], "-Z") == 0 ||
  724.        strcmp (argv[counter], "-C") == 0))
  725.     {
  726.       if (!warned_once && strcmp (argv[counter], "-C") == 0)
  727.         {
  728.           error (0, 0, _("-C is being deprecated, use -Z instead"));
  729.           warned_once = 1;
  730.         }
  731.       continue;
  732.     }
  733.  
  734.       if (walktree (check_accessibility, argv[counter]))
  735.     exit (EXIT_FAILURE);
  736.     }
  737.  
  738.   if (net_headers_mode)
  739.     {
  740.       fprintf (output, "Submitted-by: %s\n", submitter_address);
  741.       fprintf (output, "Archive-name: %s%s%02d\n\n",
  742.            archive_name, (strchr (archive_name, '/')) ? "" : "/part",
  743.            part_number ? part_number : 1);
  744.     }
  745.  
  746.   if (cut_mark_mode)
  747.     fputs (cut_mark_line, output);
  748.   fputs ("\
  749. #!/bin/sh\n",
  750.      output);
  751.   if (archive_name)
  752.     fprintf (output, "\
  753. # This is %s, a shell archive (produced by GNU %s %s)\n",
  754.          archive_name, PACKAGE, VERSION);
  755.   else
  756.     fprintf (output, "\
  757. # This is a shell archive (produced by GNU %s %s).\n",
  758.          PACKAGE, VERSION);
  759.   fputs ("\
  760. # To extract the files from this archive, save it to some FILE, remove\n\
  761. # everything before the `!/bin/sh' line above, then type `sh FILE'.\n\
  762. #\n",
  763.      output);
  764.  
  765.   time (&now);
  766.   local_time = localtime (&now);
  767.   strftime (buffer, 79, "%Y-%m-%d %H:%M %Z", local_time);
  768.   fprintf (output, "\
  769. # Made on %s by <%s>.\n",
  770.        buffer, submitter_address);
  771.  
  772.   current_directory = xgetcwd ();
  773.   if (current_directory)
  774.     {
  775.       fprintf (output, "\
  776. # Source directory was `%s'.\n",
  777.            current_directory);
  778.       free (current_directory);
  779.     }
  780.   else
  781.     error (0, errno, _("Cannot get current directory name"));
  782.  
  783.   if (check_existing_mode)
  784.     fputs ("\
  785. #\n\
  786. # Existing files will *not* be overwritten unless `-c' is specified.\n",
  787.        output);
  788.   else if (query_user_mode)
  789.     fputs ("\
  790. #\n\
  791. # existing files MAY be overwritten\n",
  792.        output);
  793.   else
  794.     fputs ("\
  795. #\n\
  796. # existing files WILL be overwritten\n",
  797.        output);
  798.  
  799.   if (query_user_mode)
  800.     fputs ("\
  801. # The unsharer will be INTERACTIVELY queried.\n",
  802.        output);
  803.  
  804.   if (vanilla_operation_mode)
  805.     {
  806.       fputs ("\
  807. # This format requires very little intelligence at unshar time.\n\
  808. # ",
  809.          output);
  810.       if (check_existing_mode || split_file_mode)
  811.     fputs ("\"if test\", ", output);
  812.       if (split_file_mode)
  813.     fputs ("\"cat\", \"rm\", ", output);
  814.       fputs ("\"echo\", \"mkdir\", and \"sed\" may be needed.\n", output);
  815.     }
  816.  
  817.   if (split_file_mode)
  818.     {
  819.  
  820.       /* May be split, explain.  */
  821.  
  822.       fputs ("#\n", output);
  823.       archive_type_position = ftell (output);
  824.       fprintf (output, "%-75s\n%-75s\n", "#", "#");
  825.     }
  826.  
  827.   fputs ("\
  828. #\n\
  829. # This shar contains:\n\
  830. # length mode       name\n\
  831. # ------ ---------- ------------------------------------------\n",
  832.      output);
  833.  
  834.   for (counter = 0; counter < argc; counter++)
  835.     {
  836.  
  837.       /* Output names of files but not parameters.  */
  838.  
  839.       if (intermixed_parameter_mode &&
  840.       (strcmp (argv[counter], "-B") == 0 ||
  841.        strcmp (argv[counter], "-T") == 0 ||
  842.        strcmp (argv[counter], "-M") == 0 ||
  843.        strcmp (argv[counter], "-z") == 0 ||
  844.        strcmp (argv[counter], "-Z") == 0 ||
  845.        strcmp (argv[counter], "-C") == 0))
  846.     continue;
  847.  
  848.       if (walktree (generate_one_header_line, argv[counter]))
  849.     exit (EXIT_FAILURE);
  850.     }
  851.   fputs ("#\n", output);
  852.  
  853.   generate_configure ();
  854.  
  855.   if (split_file_mode)
  856.     {
  857.  
  858.       /* Now check the sequence.  */
  859.  
  860.       fprintf (output, "\
  861. if test -r _sh%05d/seq; then\n\
  862.   $echo '%s'\n\
  863.   $echo '%s' '`cat _sh%05d/seq`' '%s'\n\
  864.   exit 1\n\
  865. fi\n",
  866.            sharpid,
  867.            N_("Must unpack archives in sequence!"),
  868.            N_("Please unpack part"), sharpid, N_("next!")
  869. );
  870.     }
  871. }
  872.  
  873. /* Prepare a shar script.  */
  874.  
  875. /*---.
  876. | ?  |
  877. `---*/
  878.  
  879. static int
  880. shar (local_name, restore_name)
  881.      const char *local_name;
  882.      const char *restore_name;
  883. {
  884.   char buffer[BUFSIZ];
  885.   FILE *input;
  886.   long current_size;
  887.   long remaining_size;
  888.   int split_flag = 0;        /* file split flag */
  889.   const char *file_type;    /* text of binary */
  890.   const char *file_type_remote;    /* text or binary, avoiding locale */
  891.   struct tm *restore_time;
  892.  
  893.   /* Check to see that this is still a regular file and readable.  */
  894.  
  895.   if (!S_ISREG (struct_stat.st_mode & S_IFMT))
  896.     {
  897.       error (0, 0, _("%s: Not a regular file"), local_name);
  898.       return 1;
  899.     }
  900.   if (access (local_name, 4))
  901.     {
  902.       error (0, 0, _("Cannot access %s"), local_name);
  903.       return 1;
  904.     }
  905.  
  906.   /* If file_size_limit set, get the current output length.  */
  907.  
  908.   if (file_size_limit)
  909.     {
  910.       current_size = ftell (output);
  911.       remaining_size = (file_size_limit * 1024L) - current_size;
  912.       DEBUG_PRINT (_("In shar: remaining size %ld\n"), remaining_size);
  913.  
  914.       if (!split_file_mode && current_size > first_file_position
  915.       && ((uuencoded_file_mode
  916.            ? struct_stat.st_size + struct_stat.st_size / 3
  917.            : struct_stat.st_size)
  918.           > remaining_size))
  919.     {
  920.  
  921.       /* Change to another file.  */
  922.  
  923.       DEBUG_PRINT (_("Newfile, remaining %ld, "), remaining_size);
  924.       DEBUG_PRINT (_("Limit still %d\n"), file_size_limit);
  925.  
  926.       /* Close the "&&" and report an error if any of the above
  927.          failed.  */
  928.  
  929.       /* The following code is somewhat unreadable because marking
  930.          the strings is necessary.  The complete string is (nice
  931.          formatted:
  932. : || $echo 'restore of %s failed'\n\
  933. echo 'End of part %d, continue with part %d'\n\
  934. exit 0\n",
  935.            */
  936.       fprintf (output, "\
  937. : || $echo '%s' '%s' '%s'\n\
  938. $echo '%s' '%d,' '%s' '%d'\n\
  939. exit 0\n",
  940.            N_("restore of"), restore_name, N_("failed"),
  941.            N_("End of part"), part_number,
  942.            N_("continue with part"), part_number + 1);
  943.  
  944.       close_output ();
  945.  
  946.       /* Clear mkdir_already in case the user unshars out of order.  */
  947.  
  948.       while (mkdir_already_count > 0)
  949.         free (mkdir_already[--mkdir_already_count]);
  950.  
  951.       /* Form the next filename.  */
  952.  
  953.       open_output ();
  954.       if (!quiet_mode)
  955.         fprintf (stderr, _("Starting file %s\n"), output_filename);
  956.  
  957.       if (net_headers_mode)
  958.         {
  959.           fprintf (output, "Submitted-by: %s\n", submitter_address);
  960.           fprintf (output, "Archive-name: %s%s%02d\n\n", archive_name,
  961.                strchr (archive_name, '/') ? "" : "/part",
  962.                part_number ? part_number : 1);
  963.         }
  964.  
  965.       if (cut_mark_mode)
  966.         fputs (cut_mark_line, output);
  967.  
  968.       fprintf (output, "\
  969. #!/bin/sh\n\
  970. # This is part %02d of %s.\n",
  971.            part_number,
  972.            archive_name ? archive_name : "a multipart archive");
  973.  
  974.       generate_configure ();
  975.  
  976.       first_file_position = ftell (output);
  977.     }
  978.     }
  979.   else
  980.     remaining_size = 0;        /* give some value to the variable */
  981.  
  982.   fprintf (output, "\
  983. # ============= %s ==============\n",
  984.        restore_name);
  985.  
  986.   generate_mkdir_script (restore_name);
  987.  
  988.   if (struct_stat.st_size == 0)
  989.     {
  990.       file_type = _("empty");
  991.       file_type_remote = N_("(empty)");
  992.       input = NULL;        /* give some value to the variable */
  993.     }
  994.   else
  995.     {
  996.  
  997.       /* If mixed, determine the file type.  */
  998.  
  999.       if (mixed_uuencoded_file_mode)
  1000.     {
  1001.  
  1002.       /* Uuencoded was once decided through calling the `file'
  1003.          program and studying its output: the method was slow and
  1004.          error prone.  There is only one way of doing it correctly,
  1005.          and this is to read the input file, seeking for one binary
  1006.          character.  Considering the average file size, even reading
  1007.          the whole file (if it is text) would be usually faster than
  1008.          calling `file'.  */
  1009.  
  1010.       int character;
  1011.       int line_length;
  1012.  
  1013.       if (input = fopen (local_name, "rb"), input == NULL)
  1014.         {
  1015.           error (0, errno, _("Cannot open file %s"), local_name);
  1016.           return 1;
  1017.         }
  1018.  
  1019.       /* Assume initially that the input file is text.  Then try to prove
  1020.          it is binary by looking for binary characters or long lines.  */
  1021.  
  1022.       uuencoded_file_mode = 0;
  1023.       line_length = 0;
  1024.       while (character = getc (input), character != EOF)
  1025.         if (character == '\n')
  1026.           line_length = 0;
  1027.         else if (
  1028. #ifdef __CHAR_UNSIGNED__
  1029.              byte_is_binary[character]
  1030. #else
  1031.              byte_is_binary[character & 0xFF]
  1032. #endif
  1033.              || line_length == MAXIMUM_NON_BINARY_LINE)
  1034.           {
  1035.         uuencoded_file_mode = 1;
  1036.         break;
  1037.           }
  1038.         else
  1039.           line_length++;
  1040.       fclose (input);
  1041.  
  1042.       /* Text files should terminate by an end of line.  */
  1043.  
  1044.       if (line_length > 0)
  1045.         uuencoded_file_mode = 1;
  1046.     }
  1047.  
  1048.       if (uuencoded_file_mode)
  1049.     {
  1050.       static int pid, pipex[2];
  1051.  
  1052.       file_type = (compressed_file_mode ? _("compressed")
  1053.                : gzipped_file_mode ? _("gzipped") : _("binary"));
  1054.       file_type_remote = (compressed_file_mode ? N_("(compressed)")
  1055.                   : gzipped_file_mode ? N_("(gzipped)")
  1056.                     : N_("(binary)"));
  1057.  
  1058.       /* Fork a uuencode process.  */
  1059.  
  1060.       pipe (pipex);
  1061.       fflush (output);
  1062.  
  1063.       if (pid = fork (), pid != 0)
  1064.         {
  1065.  
  1066.           /* Parent, create a file to read.  */
  1067.  
  1068.           if (pid < 0)
  1069.         error (EXIT_FAILURE, errno, _("Could not fork"));
  1070.           close (pipex[1]);
  1071.           input = fdopen (pipex[0], "r");
  1072.           if (!input)
  1073.         {
  1074.           error (0, errno, _("File %s (%s)"), local_name, file_type);
  1075.           return 1;
  1076.         }
  1077.         }
  1078.       else
  1079.         {
  1080.  
  1081.           /* Start writing the pipe with encodes.  */
  1082.  
  1083.           FILE *outptr;
  1084.  
  1085.           if (compressed_file_mode)
  1086.         {
  1087.           sprintf (buffer, "compress -b%d < '%s'",
  1088.                bits_per_compressed_byte, local_name);
  1089.           input = popen (buffer, "r");
  1090.         }
  1091.           else if (gzipped_file_mode)
  1092.         {
  1093.           sprintf (buffer, "gzip -%d < '%s'",
  1094.                gzip_compression_level, local_name);
  1095.           input = popen (buffer, "r");
  1096.         }
  1097.           else
  1098.         input = fopen (local_name, "rb");
  1099.  
  1100.           outptr = fdopen (pipex[1], "w");
  1101.           fputs ("begin 600 ", outptr);
  1102.           if (compressed_file_mode)
  1103.         fprintf (outptr, "_sh%05d/cmp\n", sharpid);
  1104.           else if (gzipped_file_mode)
  1105.         fprintf (outptr, "_sh%05d/gzi\n", sharpid);
  1106.           else
  1107.         fprintf (outptr, "%s\n", restore_name);
  1108.           copy_file_encoded (input, outptr);
  1109.           fprintf (outptr, "end\n");
  1110.           if (compressed_file_mode || gzipped_file_mode)
  1111.         pclose (input);
  1112.           else
  1113.         fclose (input);
  1114.  
  1115.           exit (EXIT_SUCCESS);
  1116.         }
  1117.     }
  1118.       else
  1119.     {
  1120.       file_type = _("text");
  1121.       file_type_remote = N_("(text)");
  1122.  
  1123.       input = fopen (local_name, "r");
  1124.       if (!input)
  1125.         {
  1126.           error (0, errno, _("File %s (%s)"), local_name, file_type);
  1127.           return 1;
  1128.         }
  1129.     }
  1130.     }
  1131.  
  1132.   /* Protect existing files.  */
  1133.  
  1134.   if (check_existing_mode)
  1135.     {
  1136.       fprintf (output, "\
  1137. if test -f '%s' && test \"$first_param\" != -c; then\n",
  1138.            restore_name);
  1139.  
  1140.       if (query_user_mode)
  1141.     {
  1142.       fprintf (output, "\
  1143.   case $shar_wish in\n\
  1144.     A*|a*)\n\
  1145.       $echo 'x -' %s '%s' ;;\n\
  1146.     *)\n\
  1147.       $echo $shar_n '? -' %s '%s' '%s' $shar_c\n\
  1148.       if test -n \"$shar_tty\"; then\n\
  1149.     read shar_wish < $shar_tty\n\
  1150.       else\n\
  1151.     read shar_wish\n\
  1152.       fi ;;\n\
  1153.   esac\n\
  1154.   case $shar_wish in\n\
  1155.     Q*|q*)\n\
  1156.       $echo '%s'; exit 1 ;;\n\
  1157.     A*|a*|Y*|y*)\n\
  1158.       shar_skip=no ;;\n\
  1159.     *)\n\
  1160.       shar_skip=yes ;;\n\
  1161.   esac\n\
  1162. else\n\
  1163.   shar_skip=no\n\
  1164. fi\n\
  1165. if test $shar_skip = yes; then\n\
  1166.   $echo 'x -' %s '%s'\n",
  1167.            N_("overwriting"), restore_name,
  1168.            N_("overwrite"), restore_name,
  1169.            N_("[no, yes, all, quit] (no)?"),
  1170.            N_("extraction aborted"),
  1171.            N_("SKIPPING"), restore_name);
  1172.     }
  1173.       else
  1174.     fprintf (output, "\
  1175.   $echo 'x -' %s '%s' '%s'\n",
  1176.          N_("SKIPPING"), restore_name, N_("(file already exists)"));
  1177.  
  1178.       if (split_file_mode)
  1179.     fprintf (output, "\
  1180.   rm -f _sh%05d/new\n",
  1181.          sharpid);
  1182.  
  1183.       fputs ("\
  1184. else\n",
  1185.          output);
  1186.  
  1187.       if (split_file_mode)
  1188.     fprintf (output, "\
  1189.   > _sh%05d/new\n",
  1190.          sharpid);
  1191.     }
  1192.  
  1193.   if (!quiet_mode)
  1194.     error (0, 0, _("Saving %s (%s)"), local_name, file_type);
  1195.  
  1196.   if (!quiet_unshar_mode)
  1197.     fprintf (output, "\
  1198.   $echo 'x -' %s '%s' '%s'\n",
  1199.          N_("extracting"), restore_name, file_type_remote);
  1200.  
  1201.   if (struct_stat.st_size == 0)
  1202.     {
  1203.  
  1204.       /* Just touch the file, or empty it if it exists.  */
  1205.  
  1206.       fprintf (output, "\
  1207.   > '%s' &&\n",
  1208.            restore_name);
  1209.     }
  1210.   else
  1211.     {
  1212.  
  1213.       /* Run sed for non-empty files.  */
  1214.  
  1215.       if (uuencoded_file_mode)
  1216.     {
  1217.  
  1218.       /* Run sed through uudecode (via temp file if might get split).  */
  1219.  
  1220.       fprintf (output, "\
  1221.   sed 's/^%c//' << '%s' ",
  1222.            line_prefix, here_delimiter);
  1223.       if (inhibit_piping_mode)
  1224.         fprintf (output, "> _sh%05d/uue &&\n", sharpid);
  1225.       else
  1226.         fputs ("| uudecode &&\n", output);
  1227.     }
  1228.       else
  1229.     {
  1230.  
  1231.       /* Just run it into the file.  */
  1232.  
  1233.       fprintf (output, "\
  1234.   sed 's/^%c//' << '%s' > '%s' &&\n",
  1235.            line_prefix, here_delimiter, restore_name);
  1236.     }
  1237.  
  1238.       while (fgets (buffer, BUFSIZ, input))
  1239.     {
  1240.  
  1241.       /* Output a line and test the length.  */
  1242.  
  1243.       if (!mandatory_prefix_mode
  1244.           && ISASCII (buffer[0])
  1245. #ifdef isgraph
  1246.           && isgraph (buffer[0])
  1247. #else
  1248.           && isprint (buffer[0]) && !isspace (buffer[0])
  1249. #endif
  1250.           /* Protect lines already starting with the prefix.  */
  1251.           && buffer[0] != line_prefix
  1252.  
  1253.           /* Old mail programs interpret ~ directives.  */
  1254.           && buffer[0] != '~'
  1255.  
  1256.           /* Avoid mailing lines which are just `.'.  */
  1257.           && buffer[0] != '.'
  1258.  
  1259. #if STRNCMP_IS_FAST
  1260.           && strncmp (buffer, here_delimiter, here_delimiter_length)
  1261.  
  1262.           /* unshar -e: avoid `exit 0'.  */
  1263.           && strncmp (buffer, "exit 0", 6)
  1264.  
  1265.           /* Don't let mail prepend a `>'.  */
  1266.           && strncmp (buffer, "From", 4)
  1267. #else
  1268.           && (buffer[0] != here_delimiter[0]
  1269.           || strncmp (buffer, here_delimiter, here_delimiter_length))
  1270.  
  1271.           /* unshar -e: avoid `exit 0'.  */
  1272.           && (buffer[0] != 'e' || strncmp (buffer, "exit 0", 6))
  1273.  
  1274.           /* Don't let mail prepend a `>'.  */
  1275.           && (buffer[0] != 'F' || strncmp (buffer, "From", 4))
  1276. #endif
  1277.           )
  1278.         fputs (buffer, output);
  1279.       else
  1280.         {
  1281.           fprintf (output, "%c%s", line_prefix, buffer);
  1282.           remaining_size--;
  1283.         }
  1284.  
  1285.       /* Try completing an incomplete line, but not if the incomplete
  1286.          line contains no character.  This might occur with -T for
  1287.          incomplete files, or sometimes when switching to a new file.  */
  1288.  
  1289.       if (*buffer && buffer[strlen (buffer) - 1] != '\n')
  1290.         {
  1291.           putc ('\n', output);
  1292.           remaining_size--;
  1293.         }
  1294.  
  1295.       if (split_file_mode
  1296. #if MSDOS
  1297.           /* 1 extra for CR.  */
  1298.           && (remaining_size -= (int) strlen (buffer) + 1) < 0
  1299. #else
  1300.           && (remaining_size -= (int) strlen (buffer)) < 0
  1301. #endif
  1302.           )
  1303.         {
  1304.  
  1305.           /* Change to another file.  */
  1306.  
  1307.           DEBUG_PRINT (_("Newfile, remaining %ld, "), remaining_size);
  1308.           DEBUG_PRINT (_("Limit still %d\n"), file_size_limit);
  1309.  
  1310.           fprintf (output, "%s\n", here_delimiter);
  1311.  
  1312.           /* Close the "&&" and report an error if any of the above
  1313.          failed.  */
  1314.  
  1315.           fprintf (output, "\
  1316.   : || $echo '%s' '%s' '%s'\n",
  1317.                N_("restore of"), restore_name, N_("failed"));
  1318.  
  1319.           if (check_existing_mode)
  1320.         fputs ("\
  1321. fi\n",
  1322.                output);
  1323.  
  1324.           if (quiet_unshar_mode)
  1325.         fprintf (output, "\
  1326. $echo '%s' '%d,' '%s' '%d'\n",
  1327.              N_("End of part"), part_number,
  1328.              N_("continue with part"), part_number + 1);
  1329.           else
  1330.         fprintf (output, "\
  1331. $echo '%s' '%s' '%s' '%d'\n\
  1332. $echo '%s' '%s' '%s' '%d'\n",
  1333.              N_("End of"),
  1334.              archive_name ? archive_name : N_("archive"),
  1335.              N_("part"),
  1336.              part_number,
  1337.              N_("File"), restore_name,
  1338.              N_("is continued in part"), part_number + 1);
  1339.  
  1340.           fprintf (output, "\
  1341. echo %d > _sh%05d/seq\n\
  1342. exit 0\n",
  1343.                part_number + 1, sharpid);
  1344.  
  1345.           if (part_number == 1)
  1346.         {
  1347.  
  1348.           /* Rewrite the info lines on the first header.  */
  1349.  
  1350.           fseek (output, archive_type_position, 0);
  1351.           fprintf (output, "%-75s\n%-75s\n",
  1352.                "\
  1353. # This is part 1 of a multipart archive.",
  1354.                "\
  1355. # Do not concatenate these parts, unpack them in order with `/bin/sh'.");
  1356.                }
  1357.           close_output ();
  1358.  
  1359.           /* Next! */
  1360.  
  1361.           open_output ();
  1362.  
  1363.           if (net_headers_mode)
  1364.         {
  1365.           fprintf (output, "Submitted-by: %s\n", submitter_address);
  1366.           fprintf (output, "Archive-name: %s%s%02d\n\n",
  1367.                archive_name,
  1368.                strchr (archive_name, '/') ? "" : "/part",
  1369.                part_number ? part_number : 1);
  1370.         }
  1371.  
  1372.           if (cut_mark_mode)
  1373.         fputs (cut_mark_line, output);
  1374.  
  1375.           fprintf (output, "\
  1376. #!/bin/sh\n\
  1377. # This is `%s' (part %d of %s).\n\
  1378. # Do not concatenate these parts, unpack them in order with `/bin/sh'.\n\
  1379. # File `%s' is being continued...\n\
  1380. #\n",
  1381.                basename (output_filename), part_number,
  1382.                archive_name ? archive_name : "a multipart archive",
  1383.                restore_name);
  1384.  
  1385.           generate_configure ();
  1386.  
  1387.           fprintf (output, "\
  1388. if test ! -r _sh%05d/seq; then\n\
  1389.   $echo '%s'\n\
  1390.   exit 1\n\
  1391. fi\n\
  1392. shar_sequence=`cat _sh%05d/seq`\n\
  1393. if test \"$shar_sequence\" != %d; then\n\
  1394.   $echo '%s' \"$shar_sequence\" '%s'\n\
  1395.   exit 1\n\
  1396. fi\n",
  1397.                sharpid,
  1398.                N_("Please unpack part 1 first!"),
  1399.                sharpid,
  1400.                part_number,
  1401.                N_("Please unpack part"),
  1402.                N_("next!"));
  1403.  
  1404.           if (check_existing_mode)
  1405.         if (quiet_unshar_mode)
  1406.           fprintf (output, "\
  1407. if test -f _sh%05d/new; then\n",
  1408.                sharpid);
  1409.         else
  1410.           fprintf (output, "\
  1411. if test ! -f _sh%05d/new; then\n\
  1412.   $echo 'x -' '%s' '%s'\n\
  1413. else\n",
  1414.                sharpid,
  1415.                N_("STILL SKIPPING"), restore_name);
  1416.  
  1417.           if (!quiet_mode)
  1418.         fprintf (stderr, _("Starting file %s\n"), output_filename);
  1419.           if (!quiet_unshar_mode)
  1420.         fprintf (output, "\
  1421.   $echo 'x -' '%s' '%s'\n",
  1422.              N_("continuing file"), restore_name);
  1423.           fprintf (output, "\
  1424.   sed 's/^%c//' << '%s' >> ",
  1425.                line_prefix, here_delimiter);
  1426.           if (uuencoded_file_mode)
  1427.         fprintf (output, "_sh%05d/uue &&\n", sharpid);
  1428.           else
  1429.         fprintf (output, "%s &&\n", restore_name);
  1430.           remaining_size = file_size_limit * 1024L;
  1431.           split_flag = 1;
  1432.         }
  1433.     }
  1434.  
  1435.       fclose (input);
  1436.       while (wait (NULL) >= 0)
  1437.     ;
  1438.  
  1439.       fprintf (output, "%s\n", here_delimiter);
  1440.       if (split_flag && !quiet_unshar_mode)
  1441.     fprintf (output, "\
  1442.   $echo '%s' '%s' '%s' &&\n",
  1443.          N_("File"), restore_name, N_("is complete"));
  1444.  
  1445.       /* If this file was uuencoded w/Split, decode it and drop the temp.  */
  1446.  
  1447.       if (uuencoded_file_mode && inhibit_piping_mode)
  1448.     {
  1449.       if (!quiet_unshar_mode)
  1450.         fprintf (output, "\
  1451.   $echo '%s' '%s' &&\n",
  1452.              N_("uudecoding file"), restore_name);
  1453.  
  1454.       fprintf (output, "\
  1455.   uudecode _sh%05d/uue < _sh%05d/uue &&\n",
  1456.            sharpid, sharpid);
  1457.     }
  1458.  
  1459.       /* If this file was compressed, uncompress it and drop the temp.  */
  1460.  
  1461.       if (compressed_file_mode)
  1462.     {
  1463.       if (!quiet_unshar_mode)
  1464.         fprintf (output, "\
  1465.   $echo '%s' '%s' &&\n",
  1466.              N_("uncompressing file"), restore_name);
  1467.  
  1468.       fprintf (output, "\
  1469.   compress -d < _sh%05d/cmp > '%s' &&\n",
  1470.            sharpid, restore_name);
  1471.     }
  1472.       else if (gzipped_file_mode)
  1473.     {
  1474.       if (!quiet_unshar_mode)
  1475.         fprintf (output, "\
  1476.   $echo '%s' '%s' &&\n",
  1477.              N_("gunzipping file"), restore_name);
  1478.  
  1479.       fprintf (output, "\
  1480.   gzip -d < _sh%05d/gzi > '%s' &&\n",
  1481.            sharpid, restore_name);
  1482.     }
  1483.     }
  1484.  
  1485.   if (timestamp_mode)
  1486.     {
  1487.  
  1488.       /* Set the dates as they were.  */
  1489.  
  1490.       restore_time = localtime (&struct_stat.st_mtime);
  1491.       fprintf (output, "\
  1492.   $shar_touch -am %02d%02d%02d%02d%02d '%s' &&\n",
  1493.            restore_time->tm_mon + 1, restore_time->tm_mday,
  1494.            restore_time->tm_hour, restore_time->tm_min,
  1495.            restore_time->tm_year, restore_name);
  1496.     }
  1497.  
  1498.   if (vanilla_operation_mode)
  1499.     {
  1500.  
  1501.       /* Close the "&&" and report an error if any of the above
  1502.      failed.  */
  1503.  
  1504.       fprintf (output, "\
  1505.   : || $echo '%s' '%s' '%s'\n",
  1506.            N_("restore of"), restore_name, N_("failed"));
  1507.     }
  1508.   else
  1509.     {
  1510.       unsigned char md5buffer[16];
  1511.       FILE *fp = NULL;
  1512.       int did_md5 = 0;
  1513.  
  1514.       /* Set the permissions as they were.  */
  1515.  
  1516.       fprintf (output, "\
  1517.   chmod %04o '%s' ||\n",
  1518.            (unsigned) (struct_stat.st_mode & 0777), restore_name);
  1519.  
  1520.       /* Report an error if any of the above failed.  */
  1521.  
  1522.       fprintf (output, "\
  1523.   $echo '%s' '%s' '%s'\n",
  1524.            N_("restore of"), restore_name, N_("failed"));
  1525.  
  1526.       if (md5_count_mode && (fp = fopen (local_name, "r")) != NULL
  1527.       && md5_stream (fp, md5buffer) == 0)
  1528.     {
  1529.       /* Validate the transferred file using `md5sum' command.  */
  1530.       size_t cnt;
  1531.       did_md5 = 1;
  1532.  
  1533.       fprintf (output, "\
  1534.   if ( %s --help 2>&1 | grep 'sage: md5sum \\[' ) >/dev/null 2>&1 \\\n\
  1535.   && ( %s --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then\n\
  1536.     %s -c << %s >/dev/null 2>&1 \\\n\
  1537.     || $echo '%s:' '%s'\n",
  1538.            MD5SUM_COMMAND, MD5SUM_COMMAND, MD5SUM_COMMAND,
  1539.            DEFAULT_HERE_DELIMITER, local_name, N_("MD5 check failed"));
  1540.  
  1541.       for (cnt = 0; cnt < 16; ++cnt)
  1542.         fprintf (output, "%02x", md5buffer[cnt]);
  1543.  
  1544.       fprintf (output, " %c%s\n\
  1545. %s\n",
  1546.            ' ', local_name, DEFAULT_HERE_DELIMITER);
  1547.       /* This  ^^^ space is not necessarily a parameter now.  But it
  1548.          is a flag for binary/text mode and will perhaps be used later.  */
  1549.     }
  1550.  
  1551.       if (fp != NULL)
  1552.     fclose (fp);
  1553.  
  1554.       if (character_count_mode)
  1555.     {
  1556.       /* Validate the transferred file using simple `wc' command.  */
  1557.  
  1558.       FILE *pfp;
  1559.       char command[BUFSIZ];
  1560.  
  1561.       sprintf (command, "%s '%s'", CHARACTER_COUNT_COMMAND, local_name);
  1562.       if (pfp = popen (command, "r"), pfp)
  1563.         {
  1564.           char wc[BUFSIZ];
  1565.           const char *prefix = "";
  1566.  
  1567.           if (did_md5)
  1568.         {
  1569.           fputs ("  else\n", output);
  1570.           prefix = "  ";
  1571.         }
  1572.  
  1573.           fscanf (pfp, "%s", wc);
  1574.           fprintf (output, "\
  1575. %s  shar_count=\"`%s '%s'`\"\n\
  1576. %s  test %s -eq \"$shar_count\" ||\n\
  1577. %s  $echo '%s:' '%s' '%s,' '%s' \"$shar_count!\"\n",
  1578.                prefix, CHARACTER_COUNT_COMMAND, restore_name,
  1579.                prefix, wc,
  1580.                prefix, restore_name, N_("original size"), wc,
  1581.                N_("current size"));
  1582.           pclose (pfp);
  1583.         }
  1584.     }
  1585.       if (did_md5)
  1586.     fputs ("  fi\n", output);
  1587.     }
  1588.  
  1589.   /* If the exists option is in place close the if.  */
  1590.  
  1591.   if (check_existing_mode)
  1592.     fputs ("\
  1593. fi\n",
  1594.        output);
  1595.  
  1596.   return 0;
  1597. }
  1598.  
  1599. /* Main control.  */
  1600.  
  1601. /*-----------------------------------------------------------------------.
  1602. | Set file mode, accepting a parameter 'M' for mixed uuencoded mode, 'B' |
  1603. | for uuencoded mode, 'z' for gzipped mode or 'Z' for compressed mode.     |
  1604. | Any other value yields text mode.                     |
  1605. `-----------------------------------------------------------------------*/
  1606.  
  1607. static void
  1608. set_file_mode (mode)
  1609.      int mode;
  1610. {
  1611.   mixed_uuencoded_file_mode = mode == 'M';
  1612.   uuencoded_file_mode = mode == 'B';
  1613.   gzipped_file_mode = mode == 'z';
  1614.   compressed_file_mode = mode == 'Z';
  1615.  
  1616.   if (gzipped_file_mode || compressed_file_mode)
  1617.     uuencoded_file_mode = 1;
  1618. }
  1619.  
  1620. /*-------------------------------------------.
  1621. | Open the next output file, or die trying.  |
  1622. `-------------------------------------------*/
  1623.  
  1624. static void
  1625. open_output ()
  1626. {
  1627.   sprintf (output_filename, output_base_name, ++part_number);
  1628.   output = fopen (output_filename, "w");
  1629.   if (!output)
  1630.     error (EXIT_FAILURE, errno, _("Opening `%s'"), output_filename);
  1631. }
  1632.  
  1633. /*-----------------------------------------------.
  1634. | Close the current output file, or die trying.     |
  1635. `-----------------------------------------------*/
  1636.  
  1637. static void
  1638. close_output ()
  1639. {
  1640.   if (fclose (output) != 0)
  1641.     error (EXIT_FAILURE, errno, _("Closing `%s'"), output_filename);
  1642. }
  1643.  
  1644. /*----------------------------------.
  1645. | Output a command format message.  |
  1646. `----------------------------------*/
  1647.  
  1648. static void
  1649. usage (status)
  1650.      int status;
  1651. {
  1652.   if (status != EXIT_SUCCESS)
  1653.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  1654.          program_name);
  1655.   else
  1656.     {
  1657.       printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
  1658.       fputs (_("\
  1659. Mandatory arguments to long options are mandatory for short options too.\n"),
  1660.          stdout);
  1661.       fputs (_("\
  1662. \n\
  1663. Giving feedback:\n\
  1664.       --help              display this help and exit\n\
  1665.       --version           output version information and exit\n\
  1666.   -q, --quiet, --silent   do not output verbose messages locally\n\
  1667. \n\
  1668. Selecting files:\n\
  1669.   -p, --intermix-type     allow -[BTzZ] in file lists to change mode\n\
  1670.   -S, --stdin-file-list   read file list from standard input\n\
  1671. \n\
  1672. Splitting output:\n\
  1673.   -o, --output-prefix=PREFIX    output to file PREFIX.01 through PREFIX.NN\n\
  1674.   -l, --whole-size-limit=SIZE   split archive, not files, to SIZE kilobytes\n\
  1675.   -L, --split-size-limit=SIZE   split archive, or files, to SIZE kilobytes\n"),
  1676.          stdout);
  1677.       fputs (_("\
  1678. \n\
  1679. Controlling the shar headers:\n\
  1680.   -n, --archive-name=NAME   use NAME to document the archive\n\
  1681.   -s, --submitter=ADDRESS   override the submitter name\n\
  1682.   -a, --net-headers         output Submitted-by: & Archive-name: headers\n\
  1683.   -c, --cut-mark            start the shar with a cut line\n\
  1684. \n\
  1685. Selecting how files are stocked:\n\
  1686.   -M, --mixed-uuencode         dynamically decide uuencoding (default)\n\
  1687.   -T, --text-files             treat all files as text\n\
  1688.   -B, --uuencode               treat all files as binary, use uuencode\n\
  1689.   -z, --gzip                   gzip and uuencode all files\n\
  1690.   -g, --level-for-gzip=LEVEL   pass -LEVEL (default 9) to gzip\n\
  1691.   -Z, --compress               compress and uuencode all files\n\
  1692.   -b, --bits-per-code=BITS     pass -bBITS (default 12) to compress\n"),
  1693.          stdout);
  1694.       fputs (_("\
  1695. \n\
  1696. Protecting against transmission:\n\
  1697.   -w, --no-character-count      do not use `wc -c' to check size\n\
  1698.   -D, --no-md5-digest           do not use `md5sum' digest to verify\n\
  1699.   -F, --force-prefix            force the prefix character on every line\n\
  1700.   -d, --here-delimiter=STRING   use STRING to delimit the files in the shar\n\
  1701. \n\
  1702. Producing different kinds of shars:\n\
  1703.   -V, --vanilla-operation   produce very simple and undemanding shars\n\
  1704.   -P, --no-piping           exclusively use temporary files at unshar time\n\
  1705.   -x, --no-check-existing   blindly overwrite existing files\n\
  1706.   -X, --query-user          ask user before overwriting files (not for Net)\n\
  1707.   -m, --no-timestamp        do not restore file modification dates & times\n\
  1708.   -Q, --quiet-unshar        avoid verbose messages at unshar time\n\
  1709.   -f, --basename            restore in one directory, despite hierarchy\n\
  1710.       --no-i18n             do not produce internationalized shell script\n"),
  1711.          stdout);
  1712.       fputs (_("\
  1713. \n\
  1714. Option -o is required with -l or -L, option -n is required with -a.\n\
  1715. Option -g implies -z, option -b implies -Z.\n"),
  1716.          stdout);
  1717.     }
  1718.   exit (status);
  1719. }
  1720.  
  1721. /*--------------------------------------.
  1722. | Decode options and launch execution.  |
  1723. `--------------------------------------*/
  1724.  
  1725. static const struct option long_options[] =
  1726. {
  1727.   {"archive-name", required_argument, NULL, 'n'},
  1728.   {"basename", no_argument, NULL, 'f'},
  1729.   {"bits-per-code", required_argument, NULL, 'b'},
  1730.   {"compress", no_argument, NULL, 'Z'},
  1731.   {"cut-mark", no_argument, NULL, 'c'},
  1732.   {"force-prefix", no_argument, NULL, 'F'},
  1733.   {"gzip", no_argument, NULL, 'z'},
  1734.   {"here-delimiter", required_argument, NULL, 'd'},
  1735.   {"intermix-type", no_argument, NULL, 'p'},
  1736.   {"level-for-gzip", required_argument, NULL, 'g'},
  1737.   {"mixed-uuencode", no_argument, NULL, 'M'},
  1738.   {"net-headers", no_argument, NULL, 'a'},
  1739.   {"no-character-count", no_argument, &character_count_mode, 0},
  1740.   {"no-check-existing", no_argument, NULL, 'x'},
  1741.   {"no-i18n", no_argument, &no_i18n, 1},
  1742.   {"no-md5-digest", no_argument, &md5_count_mode, 0},
  1743.   {"no-piping", no_argument, NULL, 'P'},
  1744.   {"no-timestamp", no_argument, NULL, 'm'},
  1745.   {"output-prefix", required_argument, NULL, 'o'},
  1746.   {"print-text-domain-dir", no_argument, &print_text_dom_dir, 1},
  1747.   {"query-user", no_argument, NULL, 'X'},
  1748.   {"quiet", no_argument, NULL, 'q'},
  1749.   {"quiet-unshar", no_argument, NULL, 'Q'},
  1750.   {"split-size-limit", required_argument, NULL, 'L'},
  1751.   {"stdin-file-list", no_argument, NULL, 'S'},
  1752.   {"submitter", required_argument, NULL, 's'},
  1753.   {"text-files", no_argument, NULL, 'T'},
  1754.   {"uuencode", no_argument, NULL, 'B'},
  1755.   {"vanilla-operation", no_argument, NULL, 'V'},
  1756.   {"whole-size-limit", required_argument, NULL, 'l'},
  1757.  
  1758.   {"help", no_argument, &show_help, 1},
  1759.   {"version", no_argument, &show_version, 1},
  1760.  
  1761.   { NULL, 0, NULL, 0 },
  1762. };
  1763.  
  1764. /*---.
  1765. | ?  |
  1766. `---*/
  1767.  
  1768. int
  1769. main (argc, argv)
  1770.      int argc;
  1771.      char *const *argv;
  1772. {
  1773.   int status = EXIT_SUCCESS;
  1774.   int stdin_file_list = 0;
  1775.   int optchar;
  1776.  
  1777.   program_name = argv[0];
  1778.   sharpid = (int) getpid ();
  1779.   setlocale (LC_ALL, "");
  1780.  
  1781.   /* Set the text message domain.  */
  1782.   bindtextdomain (PACKAGE, LOCALEDIR);
  1783.   textdomain (PACKAGE);
  1784.  
  1785.   while (optchar = getopt_long (argc, argv,
  1786.                 "+$BCDFL:MPQSTVXZab:cd:fg:hl:mn:o:pqs:wxz",
  1787.                 long_options, NULL),
  1788.      optchar != EOF)
  1789.     switch (optchar)
  1790.       {
  1791.       case '\0':
  1792.     break;
  1793.  
  1794.       case '$':
  1795. #if DEBUG
  1796.     debugging_mode = 1;
  1797. #else
  1798.     error (0, 0, _("DEBUG was not selected at compile time"));
  1799. #endif
  1800.     break;
  1801.  
  1802.       case 'B':
  1803.     set_file_mode ('B');
  1804.     break;
  1805.  
  1806.       case 'D':
  1807.     md5_count_mode = 0;
  1808.     break;
  1809.  
  1810.       case 'F':
  1811.     mandatory_prefix_mode = 1;
  1812.     break;
  1813.  
  1814.       case 'L':
  1815.     if (file_size_limit = atoi (optarg), file_size_limit > 1)
  1816.       file_size_limit--;
  1817.     split_file_mode = file_size_limit != 0;
  1818.     inhibit_piping_mode = 1;
  1819.     DEBUG_PRINT (_("Hard limit %dk\n"), file_size_limit);
  1820.     break;
  1821.  
  1822.       case 'M':
  1823.     set_file_mode ('M');
  1824.     break;
  1825.  
  1826.       case 'P':
  1827.     inhibit_piping_mode = 1;
  1828.     break;
  1829.  
  1830.       case 'Q':
  1831.     quiet_unshar_mode = 1;
  1832.     break;
  1833.  
  1834.       case 'S':
  1835.     stdin_file_list = 1;
  1836.     break;
  1837.  
  1838.       case 'V':
  1839.     vanilla_operation_mode = 1;
  1840.     break;
  1841.  
  1842.       case 'T':
  1843.     set_file_mode ('T');
  1844.     break;
  1845.  
  1846.       case 'X':
  1847.     query_user_mode = 1;
  1848.     check_existing_mode = 1;
  1849.     break;
  1850.  
  1851.       case 'b':
  1852.     bits_per_compressed_byte = atoi (optarg);
  1853.     /* Fall through.  */
  1854.  
  1855.       case 'C':
  1856.       case 'Z':
  1857.     if (optchar == 'C')
  1858.       error (0, 0, _("-C is being deprecated, use -Z instead"));
  1859.     set_file_mode ('Z');
  1860.     break;
  1861.  
  1862.       case 'a':
  1863.     net_headers_mode = 1;
  1864.     break;
  1865.  
  1866.       case 'c':
  1867.     cut_mark_mode = 1;
  1868.     break;
  1869.  
  1870.       case 'd':
  1871.     here_delimiter = optarg;
  1872.     break;
  1873.  
  1874.       case 'f':
  1875.     basename_mode = 1;
  1876.     break;
  1877.  
  1878.       case 'h':
  1879.     usage (EXIT_SUCCESS);
  1880.     break;
  1881.  
  1882.       case 'l':
  1883.     if (file_size_limit = atoi (optarg), file_size_limit > 1)
  1884.       file_size_limit--;
  1885.     split_file_mode = 0;
  1886.     DEBUG_PRINT (_("Soft limit %dk\n"), file_size_limit);
  1887.     break;
  1888.  
  1889.       case 'm':
  1890.     timestamp_mode = 0;
  1891.     break;
  1892.  
  1893.       case 'n':
  1894.     archive_name = optarg;
  1895.     break;
  1896.  
  1897.       case 'o':
  1898.     strcpy (output_base_name, optarg);
  1899.     if (!strchr (output_base_name, '%'))
  1900.       strcat (output_base_name, ".%02d");
  1901.     part_number = 0;
  1902.     open_output ();
  1903.     break;
  1904.  
  1905.       case 'p':
  1906.     intermixed_parameter_mode = 1;
  1907.     break;
  1908.  
  1909.       case 'q':
  1910.     quiet_mode = 1;
  1911.     break;
  1912.  
  1913.       case 's':
  1914.     submitter_address = optarg;
  1915.     break;
  1916.  
  1917.       case 'w':
  1918.     character_count_mode = 0;
  1919.     break;
  1920.  
  1921.       case 'x':
  1922.     check_existing_mode = 0;
  1923.     break;
  1924.  
  1925.       case 'g':
  1926.     gzip_compression_level = atoi (optarg);
  1927.     /* Fall through.  */
  1928.  
  1929.       case 'z':
  1930.     set_file_mode ('z');
  1931.     break;
  1932.  
  1933.       default:
  1934.     usage (EXIT_FAILURE);
  1935.       }
  1936.  
  1937.   /* Internationalized shell scripts are not vanilla.  */
  1938.   if (vanilla_operation_mode)
  1939.     no_i18n = 1;
  1940.  
  1941.   if (show_version)
  1942.     {
  1943.       printf ("%s - GNU %s %s\n", program_name, PACKAGE, VERSION);
  1944.       exit (EXIT_SUCCESS);
  1945.     }
  1946.  
  1947.   if (show_help)
  1948.     usage (EXIT_SUCCESS);
  1949.  
  1950.   if (print_text_dom_dir != 0)
  1951.     {
  1952.       /* Support for internationalized shell scripts is only usable with
  1953.      GNU gettext.  If we don't use it simply mark it as not available.  */
  1954. #if !defined ENABLE_NLS || defined HAVE_CATGETS \
  1955.     || (defined HAVE_GETTEXT && !defined __USE_GNU_GETTEXT)
  1956.       exit (EXIT_FAILURE);
  1957. #else
  1958.       extern const char _nl_default_dirname[]; /* Defined in dcgettext.c  */
  1959.       puts (_nl_default_dirname);
  1960.       exit (EXIT_SUCCESS);
  1961. #endif
  1962.     }
  1963.  
  1964.   line_prefix = (here_delimiter[0] == DEFAULT_LINE_PREFIX_1
  1965.          ? DEFAULT_LINE_PREFIX_2
  1966.          : DEFAULT_LINE_PREFIX_1);
  1967.  
  1968.   here_delimiter_length = strlen (here_delimiter);
  1969.  
  1970.   if (vanilla_operation_mode)
  1971.     {
  1972.       if (mixed_uuencoded_file_mode < 0)
  1973.     set_file_mode ('T');
  1974.  
  1975.       /* Implies -m, -w, -D, -F and -P.  */
  1976.  
  1977.       timestamp_mode = 0;
  1978.       character_count_mode = 0;
  1979.       md5_count_mode = 0;
  1980.       mandatory_prefix_mode = 1;
  1981.       inhibit_piping_mode = 1;
  1982.  
  1983.       /* Forbids -X.  */
  1984.  
  1985.       if (query_user_mode)
  1986.     {
  1987.       error (0, 0, _("WARNING: No user interaction in vanilla mode"));
  1988.       query_user_mode = 0;
  1989.     }
  1990.  
  1991.       /* Diagnose if not in -T state.  */
  1992.  
  1993.       if (mixed_uuencoded_file_mode
  1994.       || uuencoded_file_mode
  1995.       || gzipped_file_mode
  1996.       || compressed_file_mode
  1997.       || intermixed_parameter_mode)
  1998.     error (0, 0, _("WARNING: Non-text storage options overridden"));
  1999.     }
  2000.  
  2001.   /* Set defaults for unset options.  */
  2002.  
  2003.   if (mixed_uuencoded_file_mode < 0)
  2004.     set_file_mode ('M');
  2005.  
  2006.   if (!submitter_address)
  2007.     submitter_address = get_submitter (NULL);
  2008.  
  2009.   if (!output)
  2010.     output = stdout;
  2011.  
  2012.   /* Maybe prepare to decide dynamically about file type.  */
  2013.  
  2014.   if (mixed_uuencoded_file_mode || intermixed_parameter_mode)
  2015.     {
  2016.       memset ((char *) byte_is_binary, 1, 256);
  2017.       byte_is_binary['\b'] = 0;
  2018.       byte_is_binary['\t'] = 0;
  2019.       byte_is_binary['\f'] = 0;
  2020.       memset ((char *) byte_is_binary + 32, 0, 127 - 32);
  2021.     }
  2022.  
  2023.   /* Maybe read file list from standard input.  */
  2024.  
  2025.   if (stdin_file_list)
  2026.     {
  2027.       char stdin_buf[258];    /* FIXME: No fix limit in GNU... */
  2028.       char **list;
  2029.       int max_argc;
  2030.  
  2031.       argc = 0;
  2032.       max_argc = 32;
  2033.       list = (char **) xmalloc (max_argc * sizeof (char *));
  2034.       stdin_buf[0] = 0;
  2035.       while (fgets (stdin_buf, sizeof (stdin_buf), stdin))
  2036.     {
  2037.       if (argc == max_argc)
  2038.         list = (char **) xrealloc (list,
  2039.                        (max_argc *= 2) * sizeof (char *));
  2040.       if (stdin_buf[0] != '\0')
  2041.         stdin_buf[strlen (stdin_buf) - 1] = 0;
  2042.       list[argc] = xstrdup (stdin_buf);
  2043.       ++argc;
  2044.       stdin_buf[0] = 0;
  2045.     }
  2046.       argv = list;
  2047.       optind = 0;
  2048.     }
  2049.  
  2050.   /* Diagnose various usage errors.  */
  2051.  
  2052.   if (optind >= argc)
  2053.     {
  2054.       error (0, 0, _("No input files"));
  2055.       usage (EXIT_FAILURE);
  2056.     }
  2057.  
  2058.   if (net_headers_mode && !archive_name)
  2059.     {
  2060.       error (0, 0, _("Cannot use -a option without -n"));
  2061.       usage (EXIT_FAILURE);
  2062.     }
  2063.  
  2064.   if (file_size_limit && !part_number)
  2065.     {
  2066.       error (0, 0, _("Cannot use -l or -L option without -o"));
  2067.       usage (EXIT_FAILURE);
  2068.     }
  2069.  
  2070.   /* Start making the archive file.  */
  2071.  
  2072.   generate_full_header (argc - optind, &argv[optind]);
  2073.  
  2074.   if (query_user_mode)
  2075.     {
  2076.       quiet_unshar_mode = 0;
  2077.       if (net_headers_mode)
  2078.     error (0, 0, _("PLEASE avoid -X shars on Usenet or public networks"));
  2079.  
  2080.       fputs ("\
  2081. shar_wish=\n",
  2082.          output);
  2083.     }
  2084.  
  2085.   first_file_position = ftell (output);
  2086.  
  2087.   /* Process positional parameters and files.  */
  2088.  
  2089.   for (; optind < argc; optind++)
  2090.     if (intermixed_parameter_mode)
  2091.       if (strcmp (argv[optind], "-B") == 0)
  2092.     set_file_mode ('B');
  2093.       else if (strcmp (argv[optind], "-T") == 0)
  2094.     set_file_mode ('T');
  2095.       else if (strcmp (argv[optind], "-M") == 0)
  2096.     set_file_mode ('M');
  2097.       else if (strcmp (argv[optind], "-z") == 0)
  2098.     set_file_mode ('z');
  2099.       else if (strcmp (argv[optind], "-Z") == 0
  2100.            || strcmp (argv[optind], "-C") == 0)
  2101.     set_file_mode ('Z');
  2102.       else
  2103.     {
  2104.       if (walktree (shar, argv[optind]))
  2105.         status = EXIT_FAILURE;
  2106.     }
  2107.     else
  2108.       {
  2109.     if (walktree (shar, argv[optind]))
  2110.       status = EXIT_FAILURE;
  2111.       }
  2112.  
  2113.   /* Delete the sequence file, if any.  */
  2114.  
  2115.   if (split_file_mode && part_number > 1)
  2116.     {
  2117.       fprintf (output, "\
  2118. $echo '%s'\n",
  2119.            N_("You have unpacked the last part"));
  2120.       if (quiet_mode)
  2121.     fprintf (stderr, _("Created %d files\n"), part_number);
  2122.     }
  2123.  
  2124.   fprintf (output, "\
  2125. rm -fr _sh%05d\n\
  2126. exit 0\n",
  2127.      sharpid);
  2128.  
  2129.   exit (status);
  2130. }
  2131.